package com.yc.java8.collecoper;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Title:<br>
 * Description: <br>
 * Author:陈圆(chenyuan@bosssoft.com.cn) <br>
 * Date:2020/8/30
 */
public class DemoTest {

    public static List<Dish> getDishs() {
        List<Dish> dishes = new ArrayList<>();
        Dish dish = new Dish();
        dish.setVegetarian(true);
        dish.setName("白菜");

        Dish dish1 = new Dish();
        dish1.setVegetarian(true);
        dish1.setName("青菜");

        Dish dish2 = new Dish();
        dish2.setVegetarian(true);
        dish2.setName("波菜");

        Dish _dish2_ = new Dish();
        _dish2_.setVegetarian(true);
        _dish2_.setName("波菜");

        Dish dish3 = new Dish();
        dish3.setVegetarian(false);
        dish3.setName("猪肉");

        Dish dish4 = new Dish();
        dish4.setVegetarian(false);
        dish4.setName("牛肉");

        Dish dish5 = new Dish();
        dish5.setVegetarian(true);
        dish5.setName(null);

        dishes.add(dish);
        dishes.add(dish1);
        dishes.add(dish2);
        dishes.add(_dish2_);
        dishes.add(dish3);
        dishes.add(dish4);
//        dishes.add(dish5);
        return dishes;
    }


    public static void main(String[] args) {
        List<Dish> dishs = getDishs();

        List<Dish> collect = dishs.stream().filter(Dish::isVegetarian).collect(Collectors.toList());

        List<Dish> collect1 = dishs.stream().filter(dish ->
                dish.isVegetarian()
                        && Optional.ofNullable(dish.getName()).orElse("")
                        .endsWith("菜"))
//                .limit(2) // 限制数量
                .distinct()
                .collect(Collectors.toList());


//        collect.stream().forEach(e -> {
//            System.out.println(e.getName());
//        });

        collect1.stream().forEach(e -> {
            System.out.println(e.getName());
        });

    }
}
